package com.lapian.eventbus;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.lapian.eventbus.bean.MessageEvent;
import com.lapian.eventbus.bean.StickyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity2 extends AppCompatActivity {

    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    public void OnReceiveViscousEventClick(View view) {
        //4.注册
        EventBus.getDefault().register(MainActivity2.class);
    }

    public void OnTheMainThreadToSendDateClick(View view) {
        //4.发送消息
        EventBus.getDefault().post(new MessageEvent("主线程发送过来的数据"));
    }

    private void initView() {
        tvResult = (TextView) findViewById(R.id.tv_result);
    }
    //3.接收粘性事件
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void StickyEventBus(StickyEvent event){
        tvResult.setText(event.msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解注册
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(MainActivity2.class);
    }
}